import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild ,ElementRef} from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzTableComponent } from 'ng-zorro-antd/table';
//导入公共依赖模块
import { ShareModule,NzMessageService } from '@app/share/share.module';
import { Router ,Route,ActivatedRoute} from '@angular/router';
// import { StoreService } from '@app/serves/app.serve';
import {getShearFile_API,putFileOrFolderToRecycle_API} from '@/api/files';
import { getStore ,setStore} from '@/utils';
export interface VirtualDataInterface {
  index: number;
  name: string;
  age: number;
  address: string;
}

@Component({
  selector: 'app-recycleBin',
  templateUrl: './myShare.component.html',
  standalone: true,
  imports: [ShareModule],
  styleUrls: ['./myShare.component.less']
})
export class MyShareComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild('virtualTable', { static: false }) nzTableComponent?: NzTableComponent<VirtualDataInterface>;
  private destroy$ = new Subject<boolean>();
  listOfData: VirtualDataInterface[] = [];
  userInfo:any={}
  constructor(private message:NzMessageService,private router:Router,private route:ActivatedRoute) {
    this.userInfo= getStore({ name: 'userInfo' })
  }

  trackByIndex(_: number, data: VirtualDataInterface): number {
    return data.index;
  }

  ngOnInit(): void {
    this.getList()
  }
  _reset(row){
    let data={
      id: row.id,
      userid: this.userInfo.userid,
      type: row.type
    }
    //  todo
    // putFileOrFolderToRecycle_API(data).then(res=>{
    //   const {code,data,msg}=res
    //   if(code!==200)  this.message.create('error',msg)
    //     this.getList()
    //     this.message.create('success',msg)
    // }).catch(err=>{
    //   this.message.create('error',err)
    // })
  }

  getList(){
    const queryP={
      userid:this.userInfo.userid,
      state:2//state:2为回收站数据
    }
    getShearFile_API(queryP).then(res=>{
      const {code,data,msg}=res
      if(code!==200){
        this.message.create('error',msg)
      }else{
        this.listOfData=data.map((item,index)=>{
          return {
            ...item,
            index:index,
            type:item.filetype||'folder',
          }
        })
      }
    }).catch(err=>{
      this.message.create('error',err)
    })
  }

  handleClick(row){
    this.router.navigate([`/work/workSpace/excel`],{
      queryParams: {
        id: row.fileid,
      },
    })
  }

  ngAfterViewInit(): void {
    this.nzTableComponent?.cdkVirtualScrollViewport?.scrolledIndexChange
      .pipe(takeUntil(this.destroy$))
      .subscribe((data: number) => {
        console.log('scroll index to', data);
      });
  }

  ngOnDestroy(): void {
    this.destroy$.next(true);
    this.destroy$.complete();
  }
}
